﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace YTCMS.Core.Security
{
    public class MD5Helper
    {
        public static string Md5(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
                return "";
            StringBuilder sb = new StringBuilder();
            MD5 md5 = MD5.Create();
            byte[] tmp_source = System.Text.Encoding.UTF8.GetBytes(source);
            byte[] tmp_hash = md5.ComputeHash(tmp_source, 0, tmp_source.Length);
            foreach (byte b in tmp_hash)
            {
                sb.Append(b.ToString("X2"));
            }
            return sb.ToString();
        }//end method

        public static string HMACMD5Encode(string str, string key)
        {
            using (HMACMD5 hmd5 = new HMACMD5())
            {
                Encoding ec = Encoding.UTF8;
                hmd5.Key = ec.GetBytes(key);
                byte[] hashBytes = hmd5.ComputeHash(ec.GetBytes(str));
                string result = Convert.ToBase64String(hashBytes);

                return result;
            }
        }
    }
}
